package com.zywl.web.controller.sys;

import com.zywl.core.beans.Agreement;
import com.zywl.core.beans.User;
import com.zywl.service.AgreementService;
import com.zywl.utils.models.AjaxResult;
import com.zywl.utils.utils.Constants;
import com.zywl.utils.utils.enums.LogTypeEnum;
import com.zywl.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/agreement/")
public class AgreementController extends BaseController {
    @Autowired
    private AgreementService agreementService;

    @GetMapping("/agreementListPage")
    public ModelAndView agreementListPage(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("agreement/agreementListPage");
        try {
            User user = (User) request.getSession().getAttribute(Constants.USER_SESSION_KEY);
            Agreement agreement =  agreementService.getById(user.getUsrAssociationId());
            mav.addObject("agreement", agreement);
        } catch (Exception e) {
            logger.error("获取数据异常", e);
        }
        return mav;
    }

    @GetMapping("/agreementYinsiPage")
    public ModelAndView agreementYinsiPage(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("agreement/agreementYinsiPage");
        try {
            User user = (User) request.getSession().getAttribute(Constants.USER_SESSION_KEY);
            Agreement agreement =  agreementService.getById(user.getUsrAssociationId());
            mav.addObject("agreement", agreement);
        } catch (Exception e) {
            logger.error("获取数据异常", e);
        }
        return mav;
    }

    @PostMapping("/agreementSubmit")
    public AjaxResult agreementSubmit(HttpServletRequest request, Agreement form) {
        AjaxResult ar = new AjaxResult();
        if (form != null) {
            try {
                User user = (User) request.getSession().getAttribute(Constants.USER_SESSION_KEY);
                if(user.getUsrAssociationId() == null){
                    ar.error("平台无法操作协议");
                    return ar;
                }
                Agreement agreement =  agreementService.getById(user.getUsrAssociationId());
                if (form.getAgmMemberExplain() != null) {
                    if(agreement == null){
                        agreement = new Agreement();
                        agreement.setAgmId(user.getUsrAssociationId());
                        agreement.setAgmMemberExplain(form.getAgmMemberExplain());
                        agreementService.save(agreement);
                        saveLog(request, LogTypeEnum.AGREEMENT.getKey(), "新增会员说明" );
                        ar.success("新增会员说明成功");
                    }else{
                        agreement.setAgmMemberExplain(form.getAgmMemberExplain());
                        agreementService.update(agreement);
                        saveLog(request, LogTypeEnum.AGREEMENT.getKey(), "会员说明修改" );
                        ar.success("修改会员说明成功");
                    }
                }
                if (form.getAgmFeeExplain() != null){
                    if(agreement == null){
                        agreement = new Agreement();
                        agreement.setAgmId(user.getUsrAssociationId());
                        agreement.setAgmFeeExplain(form.getAgmFeeExplain());
                        agreementService.save(agreement);
                        saveLog(request, LogTypeEnum.AGREEMENT.getKey(), "新增费用说明" );
                        ar.success("新增费用说明成功");
                    }else{
                        agreement.setAgmFeeExplain(form.getAgmFeeExplain());
                        agreementService.update(agreement);
                        saveLog(request, LogTypeEnum.AGREEMENT.getKey(), "费用说明修改" );
                        ar.success("修改费用说明成功");
                    }
                }
                if (form.getAgmUserAgreement() != null){
                    if(agreement == null){
                        agreement = new Agreement();
                        agreement.setAgmId(user.getUsrAssociationId());
                        agreement.setAgmUserAgreement(form.getAgmUserAgreement());
                        agreementService.save(agreement);
                        saveLog(request, LogTypeEnum.AGREEMENT.getKey(), "新增用户协议" );
                        ar.success("新增用户协议成功");
                    }else{
                        agreement.setAgmUserAgreement(form.getAgmUserAgreement());
                        agreementService.update(agreement);
                        saveLog(request, LogTypeEnum.AGREEMENT.getKey(), "费用用户协议" );
                        ar.success("修改用户协议成功");
                    }
                }
                if (form.getAgmPrivacyAgreement() != null){
                    if(agreement == null){
                        agreement = new Agreement();
                        agreement.setAgmId(user.getUsrAssociationId());
                        agreement.setAgmPrivacyAgreement(form.getAgmPrivacyAgreement());
                        agreementService.save(agreement);
                        saveLog(request, LogTypeEnum.AGREEMENT.getKey(), "新增隐私政策" );
                        ar.success("新增隐私政策成功");
                    }else{
                        agreement.setAgmPrivacyAgreement(form.getAgmPrivacyAgreement());
                        agreementService.update(agreement);
                        saveLog(request, LogTypeEnum.AGREEMENT.getKey(), "隐私政策修改" );
                        ar.success("修改隐私政策成功");
                    }
                }
                return ar;
            } catch (Exception e) {
                logger.error("修改说明异常", e);
                ar.error("修改说明异常", e);
                return ar;
            }

        }
        ar.success("你的输入为空，请检查");
        return ar;
    }
}
